package com.university.sol.service.impl;

import com.university.sol.dao.UserMapper;
import com.university.sol.pojo.User;
import com.university.sol.service.UserService;
import com.university.sol.util.MD5Util;
import com.university.sol.util.UserNumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 侯瑞阳
 * @Date 2019/6/7 0:28
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户登录业务层
     * @param userEmail 用户邮箱
     * @param userPassword 用户密码
     * @return
     */
    @Override
    public User getUserByEmailAndPassword(String userEmail, String userPassword) {
        return userMapper.getUserByEmailAndPassword(userEmail, MD5Util.MD5EncodeUtf8(userPassword));
    }

    /**
     * 检测邮箱是否被占用
     * @param userEmail 注册邮箱
     * @return
     */
    @Override
    public boolean checkEmail(String userEmail) {
        // 根据邮箱去数据库查询
        int result = userMapper.getCountByEmail(userEmail);
        if (result > 0) {
            return false;
        }
        return true;
    }

    /**
     * 将用户信息写入数据库
     * @param user
     * @return
     */
    @Override
    public int registerUser(User user) {
        User newUser = new User();
        newUser.setUserNum(UserNumUtil.userNum());
        newUser.setUserEmail(user.getUserEmail().trim());
        newUser.setUserPassword(MD5Util.MD5EncodeUtf8(user.getUserPassword().trim()));
        return userMapper.insertSelective(newUser);
    }
}
